<%@ include file="database_ini.jsp"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
<link type="text/css" href="../css/flick/jquery-ui-1.8.6.custom.css" rel="stylesheet" />
<script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui-1.8.6.custom.min.js"></script>
<script>
	
	$(document).ready(function() {
		$('#back').click(function() {
			parent.history.back();
			return false;
		});
	});
</script>

</head>
<%
	boolean error = false;

	String DROPID = request.getParameter("DROPID");
	String Loc = request.getParameter("Loc");
	String Sku = request.getParameter("Sku");
	String Lottable05 = request.getParameter("Lottable05");
	String Qty = request.getParameter("Qty");



	/*********debug*/
	System.out.println("DROPID: " + DROPID);
	System.out.println("Loc: " + Loc);
	System.out.println("Sku: " + Sku);
	System.out.println("Lottable05: " + Lottable05);
	System.out.println("Qty: " + Qty);
	/**********/

	
	int LocCount =0;
	int SkuCount =0;

	String sql0 = " select count(*) AS LocCount from LOC where Loc = ?";

	pstmt = conn.prepareStatement(sql0);
	pstmt.setString(1, Loc);
	ResultSet rs0 = pstmt.executeQuery();

	if (rs0.next()) {
		LocCount = rs0.getInt("LocCount");
	}
	rs0.close();
	pstmt.close();
	
	sql0 = " select count(*) AS SkuCount from SKU where Sku = ?";

	pstmt = conn.prepareStatement(sql0);
	pstmt.setString(1, Sku);
	rs0 = pstmt.executeQuery();

	if (rs0.next()) {
		SkuCount = rs0.getInt("SkuCount");
	}
	rs0.close();
	pstmt.close();

	if(LocCount <= 0){
		%>
		儲位並不存在!!
		<p />
		<input type="button" id="back" value="返回" />
		<%
	}
	else if(SkuCount <= 0){
		%>
		商品代碼並不存在!!
		<p />
		<input type="button" id="back" value="返回" />
		<%
	}
	else{

	String sql = "select CCKEY, max(Seq)+1  AS Seq, DisplaySystemCount from CCDET "
			+ "WHERE Status = 0 and CCDROPID = ? Group by CCKEY, DisplaySystemCount";

	pstmt = conn.prepareStatement(sql);
	pstmt.setString(1, DROPID);
	
	ResultSet rs = pstmt.executeQuery();

	String CCKEY =null;
	int Seq = 0;
	String DisplaySystemCount =null;
	if (rs.next()) {
		CCKEY = rs.getString("CCKEY");
		Seq = rs.getInt("Seq");
		DisplaySystemCount = rs.getString("DisplaySystemCount");
	}
	rs.close();
	pstmt.close();
	
	/*********debug*/
	System.out.println("CCKEY: " + CCKEY);
	System.out.println("Seq: " + Seq);
	System.out.println("DisplaySystemCount: " + DisplaySystemCount);
	/**********/
	
	
	sql = "select Storerkey, Descr, GeneralSku AS UPCCode from SKU where Sku = ? ";

	pstmt = conn.prepareStatement(sql);
	pstmt.setString(1, Sku);
	
	rs = pstmt.executeQuery();

	String Storerkey =null;
	String Descr = null;
	String UPCCode =null;
	if (rs.next()) {
		Storerkey = rs.getString("Storerkey");
		Descr = rs.getString("Descr");
		UPCCode = rs.getString("UPCCode");
	}
	
	/*********debug*/
	System.out.println("Storerkey: " + Storerkey);
	System.out.println("Descr: " + Descr);
	System.out.println("UPCCode: " + UPCCode);
	/**********/
	
	
	
	//sql = "select min(ID) AS ID  from LOTXLOCXID where Sku = ? ";
	sql = "select MIN(Lot) AS Lot from LOTXLOCXID where Sku = ? and Lot <> '' ";

	pstmt = conn.prepareStatement(sql);
	pstmt.setString(1, Sku);
	
	rs = pstmt.executeQuery();

	String Lot =null;
	String ID = null;
	if (rs.next()) {
		Lot = rs.getString("Lot");
		ID = "   ";
	}
	
	/*********debug*/
	System.out.println("Lot: " + Lot);
	System.out.println("ID: " + ID);
	/**********/
	
	Statement st = null;

	st = conn.createStatement();
	
	String sql1 ="insert into CCDET (CCDROPID, Loc, Sku, Lottable05, Qty, CCKEY, Seq, DisplaySystemCount, Storerkey, Descr, UPCCode, Lot, ID  ) VALUES ( '" + DROPID + "', '" +Loc + "', '"
	+ Sku + "' ,  '" + Lottable05 + "' ,  '" + Qty + "' ,  '" + CCKEY + "' ,  " + Seq + " ,  '" + DisplaySystemCount+ "' ,  '" + Storerkey+ "' ,  '" + Descr + "' ,  '" + UPCCode + "' ,  '" + Lot + "' ,  '" + ID + "')";
	
	
	st.executeUpdate(sql1);

	
	
	response.sendRedirect("index.jsp");
	}
	conn.close();
%>